package com.evil.enterprise.controller;

import com.evil.application.api.RemoteApplicationFormService;
import com.evil.application.api.RemoteApplicationService;
import com.evil.application.pojo.dto.form.ApplicationFormBaseReqDTO;
import com.evil.application.pojo.dto.form.ApplicationFormBaseRespDTO;
import com.evil.application.pojo.dto.form.ApplicationFormRespDTO;
import com.evil.application.pojo.dto.form.ModifyApplicationFormReqDTO;
import com.evil.common.application.dto.ApplicationKeyVersionReqDTO;
import com.evil.common.core.entity.R;
import com.evil.common.file.dto.FileRespDTO;
import com.evil.common.file.dto.UploadReqDTO;
import com.evil.common.log.annotation.RequestLogAnnotation;
import com.evil.common.file.constant.OssConstant;
import com.evil.common.file.enums.BusTypeEnum;
import com.evil.file.api.RemoteFileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 应用表单接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/application/form")
public class ApplicationFormController {

    private final RemoteFileService remoteFileService;

    private final RemoteApplicationService remoteApplicationService;

    private final RemoteApplicationFormService remoteApplicationFormService;

    /**
     * 应用表单信息
     *
     * @param applicationKeyVersionReqDTO applicationKeyVersionReqDTO
     * @return R
     */
    @PostMapping("/info")
    public R<ApplicationFormRespDTO> applicationFormInfo(@Validated @RequestBody ApplicationKeyVersionReqDTO applicationKeyVersionReqDTO) {
        return R.success(remoteApplicationFormService.applicationFormInfo(applicationKeyVersionReqDTO));
    }

    /**
     * 应用表单基础信息列表
     *
     * @param applicationFormBaseReqDTO applicationFormBaseReqDTO
     * @return R
     */
    @PostMapping("/bases")
    public R<List<ApplicationFormBaseRespDTO>> applicationFormBases(@Validated @RequestBody ApplicationFormBaseReqDTO applicationFormBaseReqDTO) {
        return R.success(remoteApplicationFormService.applicationFormBases(applicationFormBaseReqDTO));
    }

    /**
     * 编辑应用表单
     *
     * @param modifyApplicationFormReqDTO modifyApplicationFormReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/modify")
    public R<?> modifyApplicationForm(@Validated @RequestBody ModifyApplicationFormReqDTO modifyApplicationFormReqDTO) {
        remoteApplicationFormService.modifyApplicationForm(modifyApplicationFormReqDTO);
        return R.success();
    }

    /**
     * 编辑应用表单并发布
     *
     * @param modifyApplicationFormReqDTO modifyApplicationFormReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/modify-publish")
    public R<?> modifyAndPublishApplicationForm(@Validated @RequestBody ModifyApplicationFormReqDTO modifyApplicationFormReqDTO) {
        // 两次操作 独立事务
        remoteApplicationFormService.modifyApplicationForm(modifyApplicationFormReqDTO);
        remoteApplicationService.publishApplication(modifyApplicationFormReqDTO.getApplicationKey());
        return R.success();
    }

    /**
     * 上传表单附件
     *
     * @param uploadReqDTO uploadReqDTO
     * @return R
     */
    @PostMapping("/file/upload")
    public R<FileRespDTO> uploadFormFile(@Validated UploadReqDTO uploadReqDTO) {
        uploadReqDTO.setType(BusTypeEnum.APPLICATION_FORM_FILE.getId());
        uploadReqDTO.setAllowContentType(OssConstant.UPLOAD_ALLOW_MIME_TYPE);
        uploadReqDTO.setAllowMaxSize(OssConstant.UPLOAD_FILE_SIZE);
        return R.success(remoteFileService.uploadFile(uploadReqDTO));
    }
}
